//
//  TypeSelectView.m
//  ForestPack
//
//  Created by 郑洲 on 2018/7/24.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "TypeSelectView.h"

@interface TypeSelectView()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, strong) NSArray *dataArray;

@property (nonatomic, assign) NSInteger selectType;


@end

@implementation TypeSelectView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.dataArray = [NSArray array];
        self.selectType = 0;
        
        self.shadowBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        self.shadowBtn.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        self.shadowBtn.alpha = 0;
        [self.shadowBtn addTarget:self action:@selector(hidden) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.shadowBtn];
        
        self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, ScreenX375(152), SCREEN_WIDTH, SCREEN_HEIGHT - ScreenX375(152) - ScreenX375(104))];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.tableView.rowHeight = ScreenX375(44);
        [self addSubview:self.tableView];
        
        UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - ScreenX375(104), SCREEN_WIDTH, ScreenX375(104))];
        bottomView.backgroundColor = [UIColor whiteColor];
        [self addSubview:bottomView];
        
        UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(30), SCREEN_WIDTH - ScreenX375(26), ScreenX375(44))];
        sureBtn.backgroundColor = ThemeColor;
        sureBtn.layer.cornerRadius = ScreenX375(22);
        sureBtn.clipsToBounds = YES;
        [sureBtn setTitle:@"确 定" forState:UIControlStateNormal];
        [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        sureBtn.titleLabel.font = Kfont(16);
        [sureBtn addTarget:self action:@selector(done) forControlEvents:UIControlEventTouchUpInside];
        [bottomView addSubview:sureBtn];
    }
    return self;
}

- (void)hidden {
    if (_hiddenClick) {
        _hiddenClick();
    }
}

- (void)done {
    if (_doneClick) {
        _doneClick(_selectType);
    }
}

- (void)modelArrayForView:(NSArray *)array andSelect:(NSInteger)select{
    _dataArray = array;
    _selectType = select;
    [_tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TypeTableCell *cell = [TypeTableCell cellWithTableView:tableView andIndexPath:indexPath];
    [cell initWithString:_dataArray[indexPath.row]];
    if (indexPath.row == _selectType) {
        cell.selectedIcon.hidden = NO;
    }else {
        cell.selectedIcon.hidden = YES;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    _selectType = indexPath.row;
    [self.tableView reloadData];
}

@end

@interface TypeTableCell ()

@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation TypeTableCell

+ (TypeTableCell *)cellWithTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"TypeTableCell";
    TypeTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[TypeTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        [cell createSubView];
    }
    
    return cell;
}

- (void)createSubView {
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), 0, ScreenX375(330), ScreenX375(44))];
    self.titleLabel.textColor = REXADECIMALCOLOR(@"#333333");
    self.titleLabel.font = Kfont(14);
    [self addSubview:self.titleLabel];
    
    _selectedIcon = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - ScreenX375(37), ScreenX375(15), ScreenX375(22), ScreenX375(14))];
    _selectedIcon.image = [UIImage imageNamed:@"勾选红色"];
    [self addSubview:_selectedIcon];
    
    UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(44) - 0.5, SCREEN_WIDTH - ScreenX375(26), 0.5)];
    line.image = [UIImage imageNamed:@"虚线"];
    [self addSubview:line];
}

- (void)initWithString:(NSString *)title {
    self.titleLabel.text = title;
}

@end
